package dnl.dtools.grm;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

/**
 * An abstraction over all kinds of resources.
 * 
 * @author Daniel Ore
 * 
 */
public abstract class Resource  {

	protected URL url;

	public Resource(URL url) {
		this.url = url;
	}

	/* (non-Javadoc)
	 * @see dnl.dtools.grm.Resource#getName()
	 */
	public String getName() {
		String s = url.toString();
		int ind = s.lastIndexOf('/');
		if(ind != -1)
			return s.substring(ind + 1);
		return s;
	}

	public URL getUrl() {
		return url;
	}

	/* (non-Javadoc)
	 * @see dnl.dtools.grm.Resource#exists()
	 */
	public abstract boolean exists();
	
	/* (non-Javadoc)
	 * @see dnl.dtools.grm.Resource#isDirectory()
	 */
	public abstract boolean isDirectory();

	/**
	 * 
	 * @return
	 */
	public abstract boolean isArchive();

	/**
	 * 
	 * @return <code>true</code> if this <code>Resource</code> is within an
	 *         archive.
	 */
	public abstract boolean isArchiveEntry();

	/**
	 * 
	 * @return
	 */
	public abstract boolean isHidden();

	/**
	 * 
	 * @return
	 */
	public abstract boolean isLocal();

	/**
	 * 
	 * @return
	 */
	public abstract long getLastModified();

	/**
	 * 
	 * @return
	 */
	public abstract long getSize();

	/* (non-Javadoc)
	 * @see dnl.dtools.grm.Resource#list()
	 */
	public abstract Resource[] list() throws ResourceExtractionException;

	/* (non-Javadoc)
	 * @see dnl.dtools.grm.Resource#getParent()
	 */
	public abstract Resource getParent();
	/* (non-Javadoc)
	 * @see dnl.dtools.grm.Resource#getChild(java.lang.String)
	 */
	public abstract Resource getChild(String name);

	/**
	 * 
	 * 
	 * @return
	 * @throws IOException
	 */
	public abstract boolean mkDir() throws IOException;
	
	/* (non-Javadoc)
	 * @see dnl.dtools.grm.Resource#delete()
	 */
	public abstract boolean delete() throws IOException;

	/* (non-Javadoc)
	 * @see dnl.dtools.grm.Resource#renameTo(dnl.dtools.grm.Resource)
	 */
	public abstract boolean renameTo(Resource dest) throws IOException;

	/**
	 * Gets an <code>InputStream</code> to this <code>Resource</code>.
	 * 
	 * @return
	 */
	public abstract InputStream getInputStream() throws IOException;

	/**
	 * Gets an <code>OutputStream</code> to this <code>Resource</code>.
	 * Using this <code>OutputStream</code>, it is possible to create/modify
	 * the resource.
	 * 
	 * @return
	 */
	public abstract OutputStream getOutputStream() throws IOException;
}
